package khs.java_jmf_test.main.java;

import java.io.IOException;

import javax.media.CaptureDeviceInfo;
import javax.media.DataSink;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSinkException;
import javax.media.NoProcessorException;
import javax.media.Player;
import javax.media.Processor;
import javax.media.control.FormatControl;
import javax.media.control.TrackControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;

import com.sun.media.protocol.javasound.JavaSoundSourceStream;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws NoProcessorException, IOException, InterruptedException, NoDataSinkException
    {
        CaptureDeviceInfo di = null;
        Player p = null;
        
        
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, null, -1);
            boolean bJavaSound = AudioSystem.isLineSupported(info);
//            
            if(bJavaSound){
                CaptureDeviceInfo[] cdi = JavaSoundSourceStream.listCaptureDeviceInfo();
                if (cdi != null) {
                        di = cdi[0];
                        Processor processor = null;
                        processor = Manager.createProcessor(di.getLocator());
                        
                        processor.configure();
                        
                        while (processor.getState() != Processor.Configured){
                            Thread.sleep(100);
                        }
                        processor.setContentDescriptor(new ContentDescriptor( ContentDescriptor.RAW));
                        TrackControl track[] = processor.getTrackControls();
                        
                        boolean encodingOk = false;
                        
                        for (int i = 0; i < track.length; i++) {
                            if (!encodingOk && track[i] instanceof FormatControl) {
                                if (((FormatControl)track[i]).setFormat( new AudioFormat(AudioFormat.GSM_RTP,8000,16,1)) == null) {
                                    track[i].setEnabled(false);
                                }else {
                                    encodingOk = true;
                                }
                            }else {
                                track[i].setEnabled(false);
                            }
                        }
                        
                        if (encodingOk) {
                            processor.realize();
                            while (processor.getState() != Processor.Realized){
                                Thread.sleep(100);
                            }
                            
                            DataSource ds = null;
                            ds = processor.getDataOutput();
                            
                            String url= "rtp://127.0.0.1:12000/audio";
                            MediaLocator m = new MediaLocator(url);
                            DataSink d = Manager.createDataSink(ds, m);
                            d.open();
                            d.start();
                            processor.start();
                        }
                        
                        
//                  for (int i = 0; i < cdi.length; i++){
//                      System.out.println("name: " + cdi[i].getName());
//                      
//                      System.out.println("========== Locator ==========");
//                      
//                      MediaLocator mediaLocator = cdi[i].getLocator();
//                      System.out.println("protocol: " + mediaLocator.getProtocol());
//                      System.out.println("remainder: " + mediaLocator.getRemainder());
//                      System.out.println("tostring: " + mediaLocator.toString());
//                      System.out.println("externalForm: " + mediaLocator.toExternalForm());
//                      
//                      System.out.println("========== Formats ============");
//                      Format[] format = cdi[i].getFormats();
//                      for(int j=0;j<format.length;j++){
//                          System.out.println("===> " + String.valueOf(j).toString());
//                          System.out.println("encoding: " + format[i].getEncoding());
//                          System.out.println("tostring: " + format[i].toString());
//                          System.out.println("datatype: " + format[i].getDataType());
//                      }
//                  }
                }
            }
    }
}
